package org.balabala.user.controller;

import java.util.*;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import org.balabala.user.entity.FavoriteInfoEntity;
import org.balabala.user.service.FavoriteInfoService;
import org.balabala.user.utils.PageUtils;
import org.balabala.user.utils.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;





/**
 * 
 *
 * @author hanshanxi
 * @email 3176988793@qq.com
 * @date 2021-09-09 10:36:54
 */
@RestController
@RequestMapping("balabala/favorite")
public class FavoriteInfoController {
    @Autowired
    private FavoriteInfoService favoriteInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    
    public Resp list(@RequestParam Map<String, Object> params){
        PageUtils page = favoriteInfoService.queryPage(params);
        return Resp.ok().put("page", page);
    }


    /**
     * 根据用户UID查询用户喜欢的视频
     */
    @RequestMapping("/info")
    public Resp info(@RequestParam Long uid,@RequestParam Integer page, @RequestParam Integer limit){
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("page",page.toString());
        hashMap.put("uid",uid);
        hashMap.put("limit",limit.toString());

        PageUtils pageUtils =   favoriteInfoService.getListByUid(hashMap);
        if(pageUtils == null){
            return  Resp.error(500,"该用户暂无喜欢的视频");
        }else{
            return Resp.ok().put("data",pageUtils);
        }
    }

    /**
     * 保存
     */
    @RequestMapping("/save")

    public Resp save(@RequestParam Long uid,@RequestParam Long videoId){

        return favoriteInfoService.saveInfo(uid,videoId);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")

    public Resp update(@RequestBody FavoriteInfoEntity favoriteInfo){
		favoriteInfoService.updateById(favoriteInfo);

        return Resp.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")

    public Resp delete(@RequestBody Long[] ids){
		favoriteInfoService.removeByIds(Arrays.asList(ids));
        return Resp.ok();
    }

    @RequestMapping("/deleteByVideoId")
    public Resp delete(@RequestParam Long videoId){
        boolean video_id = favoriteInfoService.remove(new QueryWrapper<FavoriteInfoEntity>().eq("video_id", videoId));
        if(video_id){
            return Resp.ok();
        }else {
            return Resp.error(500,"删除失败");
        }
    }

}
